package data;

import org.apache.flink.streaming.api.functions.source.SourceFunction;
import po.Event;

import java.util.Calendar;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 测试数据源
 * @Author: ZYX
 * @Date: 2022/2/11 10:54
 * @Version: 1.0
 */
public class ClickSource implements SourceFunction<Event> {

    private boolean running = true;
    private String[] userArr = {"Alen","Bob","Carter","Dave","Eden","Frank","Gucci"};
    private String[] urlArr = {"./home","./cart","./fav","./user/1","./product/2"};
    private Random random = new Random();

    @Override
    public void run(SourceContext<Event> sourceContext) throws Exception {
        while (running){
            // collect 方法，向下游发送数据
            sourceContext.collect(
                    new Event(userArr[random.nextInt(userArr.length)],
                            urlArr[random.nextInt(urlArr.length)],
                            Calendar.getInstance().getTimeInMillis())
            );
            TimeUnit.SECONDS.sleep(1L);
        }
    }

    @Override
    public void cancel() {
        running = false;

    }
    
}
